import unittest
from time import sleep

import requests
from parameterized import parameterized

from api.regLoginApi import RegLoginApi
from common import utils
from common.utils import clear_user_info


class TestRegLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        clear_user_info()

    def setUp(self) -> None:
        self.session = requests.session()
        self.reg_login_api = RegLoginApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    # 获取图片验证码测试用例
    @parameterized.expand(utils.read_json('test_reg_login', 'img_verify_code'))
    def test01_img_verify_code(self, r, exp_status_code):
        resp = self.reg_login_api.get_img_verify_code(r)
        self.assertEqual(exp_status_code, resp.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(utils.read_json('test_reg_login', 'phone_verify_code'))
    def test02_phone_verify_code(self, phone, imgVerifyCode, type, exp_status_code, description):
        self.reg_login_api.get_img_verify_code(123456)
        resp = self.reg_login_api.get_phone_verify_code(phone, imgVerifyCode, type)
        json_data = resp.json()
        print(f'短信验证码: {json_data}')
        # self.assertEqual(200, resp.status_code)
        # self.assertIn("成功", json_data.get('description'))
        # 公共断言
        utils.common_assert(self, resp, exp_status_code, description)

    # 注册测试用例
    @parameterized.expand(utils.read_json('test_reg_login', 'urse_register'))
    def test03_register(self, phone, password, verifycode, phone_code, dy_server, exp_status_code,
                        description):
        self.reg_login_api.get_img_verify_code(123456)
        self.reg_login_api.get_phone_verify_code(phone, "8888")
        resp = self.reg_login_api.user_register(phone, password, verifycode, phone_code, dy_server)
        json_data = resp.json()
        print(f'注册: {json_data}')
        # self.assertEqual(200, resp.status_code)
        # self.assertIn('成功', json_data.get('description'))
        # 公共断言
        utils.common_assert(self, resp, exp_status_code, description)

    # 登录测试用例
    @parameterized.expand(utils.read_json('test_reg_login', 'urse_login'))
    def test04_user_login(self, keywords, password, exp_status_code, description):
        resp = self.reg_login_api.user_login(keywords, password)
        json_data = resp.json()
        print(f'登录: {json_data}')
        # self.assertEqual(200, resp.status_code)
        # self.assertIn('成功', json_data.get('description'))
        # 公共断言
        utils.common_assert(self, resp, exp_status_code, description)

    @unittest.skip
    # 登录密码错误验证
    def test05_user_login_pwd_chonk(self):
        # 密码错误1次
        resp = self.reg_login_api.user_login('15814662588', "123456")
        data_json = resp.json()
        print(f'登录密码错误验证,{data_json}')
        utils.common_assert(self, resp, 200, '密码错误1次')
        # 密码错误2次
        resp = self.reg_login_api.user_login('15814662588', "123456")
        data_json = resp.json()
        print(f'登录密码错误验证,{data_json}')
        utils.common_assert(self, resp, 200, '密码错误2次')
        # 密码错误三次
        resp = self.reg_login_api.user_login('15814662588', "123456")
        data_json = resp.json()
        print(f'登录密码错误验证,{data_json}')
        utils.common_assert(self, resp, 200, '错误密码达到上限')
        # 60s后登录成功
        sleep(60)
        resp = self.reg_login_api.user_login('15814662588', "123456a")
        data_json = resp.json()
        print(f'登录密码错误验证,{data_json}')
        utils.common_assert(self, resp, 200, '成功')
